<?php include_once ('function/check_login_function.php'); onlyValid(); onlyPatient();?>
<!DOCTYPE html>
<html>
<head>
	<title>Request</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="">
</head>
<body>
	<div id="wrap">
		<header>
		</header>
		<div id="main">
			<div id="menu">
			</div>
			<div id="content">
				
					<?php 
					if (isset($_GET['msg'])) {
						if (intval($_GET['msg']) === 1) {
							echo '<p>You have successfully removed a request.</p>';
						}
					}
					if (isset($_GET['doctor'])) {
						$doctor_id = $_GET['doctor'];
						$doctor = Doctor::findDoctor($doctor_id);
						$doctor_name = $doctor['user_firstname'] . ' ' . $doctor['user_lastname'];
						echo '<h1>' . $doctor_name . '</h1>';
						echo '<div><ul>';						
						echo '<li>' . $doctor['user_gender'] . '</li>';
						echo '<li>' . $doctor['user_email'] . '</li>';
						echo '<li>' . $doctor['user_contact'] . '</li>';
						echo '<li>' . $doctor['doctor_speciality'] . '</li>';
						echo '<li>' . $doctor['doctor_regno'] . '</li>';
						echo '</ul></div>';
						
						if ($user->checkRequestPending($doctor_id) === 0) {
							echo '<p>You are already under this doctor</p>';
					?>
						<form action="function/remove_request_function.php" method="POST">
						<input type="hidden" id="doctor" name="doctor" value="<?php echo $doctor_id; ?>"/>
						<input type="submit" name="remove" value="Remove Doctor" /> <br /> 
						</form>
					<?php } else if ($user->checkRequestPending($doctor_id) === 1) { ?>
					<?php
							echo '<p>You\'ve already sent a request. Waiting for doctor to accept.</p>';
					?>
						<form action="function/remove_request_function.php" method="POST">
						<input type="hidden" id="doctor" name="doctor" value="<?php echo $doctor_id; ?>"/>
						<input type="submit" name="remove" value="Remove Request" /> <br /> 
						</form>
					<?php } else { ?>
						<form action="function/send_request_function.php" method="POST">
						<label for="note"> Note (Optional): </label> </br>
						<textarea rows="4" cols="50" name="note" id="note"></textarea> </br>
						<input type="hidden" id="doctor" name="doctor" value="<?php echo $doctor_id; ?>"/>
						<input type="submit" name="send" value="Send Request" /> <br /> 
						</form>
						<?php } ?>
					<?php } else { ?>
						<p>List of requests</p>
						<ol>
						<?php
							$request_list = $user->getDoctorList();
							if ($request_list !== null) {
								foreach ($request_list as $request) {
									echo '<form action="function/remove_request_function.php" method="POST">';
									echo '<li>';
									echo User::findName($request['doctor_id']);
									if (intval($request['request_pending']) === 0) {
										echo ' (Accepted) ';
									} else {
										echo ' (Pending) ';
									}
									$doctor_id = $request['doctor_id'];

									echo '<input type="hidden" id="doctor" name="doctor" value="' . $doctor_id . '"/>';
									echo '<input type="submit" name="remove" value="Remove Request" /> ';
									echo '</li>';
									echo' </form>';
								}
							} else {
								echo 'You have sent no requests at this moment';
							}
						?>
						</ol>
					<?php } ?>
			</div>
		</div>
		<footer>
			<div id="footer">
				
			</div>
		</footer>
	</div>
</body>